/*
 * Copyright 2013 JROSE
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jrose.service.loader.internal;

import lombok.NoArgsConstructor;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.Validate;
import org.jrose.api.annotation.remote.ExposeHttpService;
import org.jrose.entity.constant.UserType;
import org.jrose.entity.data.Role;
import org.jrose.entity.data.Team;
import org.jrose.entity.data.User;
import org.jrose.service.loader.AbstractLoader;
import org.jrose.service.loader.LineupLoader;
import org.jrose.service.loader.RoleLoader;
import org.jrose.service.loader.TeamLoader;

import javax.inject.Named;
import java.util.List;

/**
 * Default {@link TeamLoader} implementation.
 *
 * @author Xiaohai Zhang
 * @see LineupLoader
 * @see RoleLoader
 * @since 2013-08-29 22:05
 */
@Named
@NoArgsConstructor
@ExposeHttpService(name = "/teamLoader", serviceInterface = TeamLoader.class)
public class TeamLoaderImpl extends AbstractLoader implements TeamLoader {
    @Override
    public Team transform(Team source) {
        if (source == null) {
            return null;
        }

        Team destination = new Team();
        try {
            PropertyUtils.copyProperties(destination, source);
        } catch (Exception ex) {
            throw new RuntimeException(ex.getMessage());
        }

        LineupLoader lineupLoader = getBean(LineupLoader.class);
        destination.setActiveLineup(lineupLoader.loadTeamActiveLineup(destination));
        destination.getLineups().addAll(lineupLoader.loadTeamLineups(destination));

        RoleLoader roleLoader = getBean(RoleLoader.class);
        List<Role> roles = roleLoader.loadTeamRoles(destination);
        for (Role role : roles) {
            switch (role.getRoleType()) {
                case LEADER: {
                    Validate.isTrue(destination.getLeader() == null, "Data crashed, more than 1 leader found of team %s", destination.getId());
                    destination.setLeader(role);
                    break;
                }
                case FOLLOWER: {
                    destination.getFollowers().add(role);
                }
                default: {
                    throw new UnsupportedOperationException();
                }
            }
        }
        return destination;
    }

    @Override
    public Team loadUserTeam(User user) {
        if (user == null || user.getUserType() != UserType.PLAYER) {
            if (user != null) {
                log.warn("User '{}' has no team", user.getId());
            }
            return null;
        }
        return transform(teamPersistence.findByUserId(user.getId()));
    }
}
